#!/usr/bin/python3

import polib
import sys

SECTIONS = {300: ["en/git-init.txt",
                  "en/git-clone.txt",
                  "en/urls.txt",
                  "en/git-add.txt"],
            280: ["en/git-status.txt",
                  "en/git-diff.txt",
                  "en/diff-options.txt",
                  "en/diff-format.txt",
                  "en/git-commit.txt",
                  "en/date-formats.txt",
                  "en/i18n.txt",
                  "en/git-reset.txt",
                  "en/git-rm.txt",
                  "en/git-mv.txt"],
            260: ["en/git-show.txt",
                  "en/pretty-options.txt",
                  "en/pretty-formats.txt",
                  "en/diff-generate-patch.txt",
                  "en/git-log.txt",
                  "en/line-range-format",
                  "en/rev-list-options.txt",
                  "en/git-shortlog.txt",
                  "en/mailmap.txt",
                  "en/git-describe.txt"],
            240: ["en/git-branch.txt",
                  "en/git-checkout.txt",
                  "en/git-merge.txt",
                  "en/merge-options.txt",
                  "en/merge-strategies.txt",
                  "en/config/merge.txt",
                  "en/config/fmt-merge-msg.txt",
                  "en/mergetools-merge.txt",
                  "en/git-mergetool.txt",
                  "en/git-stash.txt",
                  "en/git-tag.txt",
                  "en/git-worktree.txt"],
            220: ["en/git-fetch.txt",
                  "en/fetch-options.txt",
                  "en/pull-fetch-param.txt",
                  "en/urls-remotes.txt",
                  "en/transfer-data-leaks.txt",
                  "en/git-pull.txt",
                  "en/git-push.txt",
                  "en/git-remote.txt",
                  "en/git-submodule.txt"]
            }

def main (f):
    po = polib.pofile(f, wrapwidth=0)
    priorities = dict((source, section) for section in SECTIONS for source in SECTIONS[section])
    for entry in po:
      if not entry.obsolete:
        flags = [f for f in entry.flags if not (f.startswith("priority") or f.isdigit()) ]
        p = max(priorities.get(filename) or 100 for (filename,_) in entry.occurrences)
        flags.append("priority:{}".format(p))
        entry.flags = flags
    po.save(f)


if __name__ == '__main__':
    if len(sys.argv)>1:
        main(sys.argv[1])
